<?php
/**
 * Created by PhpStorm.
 * FileName: AirwayController.class.php
 * User: Administrator
 * Date: 2018/9/11
 * Time: 16:59
 */

namespace Admin\Controller;

use Admin\Controller\AdminController;
use Admin\Model\OrderModel;
use Admin\Model\UserModel;

/**
 * Class AirwayController
 * @package Admin\Controller
 * @todo 订单管理模块
 * @menu 订单管理
 * @author fanyuliang
 * @menuAuto index
 * @order 501
 * @icon article.png
 */
class OrderController extends AdminController {

    private $orderModel;
    private $userModel;

    function __construct() {
        parent::__construct();
        $this->orderModel = new OrderModel();
        $this->userModel = new UserModel();
    }

    /**
     * @todo 订单列表
     * @menu 订单列表
     * @order 1
     */
    public function index() {
        $this->setForm();
        $where = array();
        if(IS_POST){
            if(I('buyer_id')){
                $where['buyer_id'] = I('buyer_id');
                $searchUserName = $this->userModel->where(array('id' => $where['buyer_id']))->getField('real_name');
                $this->assign('searchBuyerName',$searchUserName);
            }
            if(I('seller_id')){
                $where['seller_id'] = I('seller_id');
                $searchUserName = $this->userModel->where(array('id' => $where['seller_id']))->getField('real_name');
                $this->assign('searchSellerName',$searchUserName);
            }
            $order_from_date = I('order_from_date');
            $order_to_date = I('order_to_date');
            if ($order_from_date && $order_to_date) {
                $where['add_time'] = array('between',sprintf('%s,%s',strtotime(sprintf('%s 00:00:00',$order_from_date)),strtotime(sprintf('%s 23:59:59',$order_to_date))));
                $this->assign('order_from_date',strtotime($order_from_date));
                $this->assign('order_to_date',strtotime($order_to_date));
            } else if ($order_from_date) {
                $where['add_time'] = array('egt',strtotime(sprintf('%s 00:00:00',$order_from_date)));
                $this->assign('order_from_date',strtotime($order_from_date));
            } else if ($order_to_date) {
                $where['add_time'] = array('elt',strtotime(sprintf('%s 23:59:59',$order_to_date)));
                $this->assign('order_to_date',strtotime($order_to_date));
            }
            if(I('type') != ''){
                $where['type'] = I('type');
            }
            if(I('state') != ''){
                $where['state'] = I('state');
            }
        }
        $pageSize = 15;
        $data = $this->orderModel->getList($where,$pageSize);
        $this->setPageData($data);
        $this->display();
    }

    /**
     * @todo 订单明细
     */
    public function detail() {
        $id = I('id');
        $row = $this->orderModel->detail(array('id' => $id));
        $row['order_no'] = sprintf('%s%s',date('YmdHis',$row['add_time']),sprintf('%011s',$row['id']));
        $row['totalAmount'] = $row['reward']+$row['deposit']+$row['brokerage'];
        $this->assign('row',$row);
        $this->display();
    }
}